def dict_base():
    """
    字典的基本操作1
    :return:
    """
    xiaoming_dict = {"name": "小明"}
    # 1取值
    print(xiaoming_dict["name"])
    # 2增加修改
    xiaoming_dict["age"] = 18
    print(xiaoming_dict)
    xiaoming_dict['name'] = '小小明'
    print(xiaoming_dict)
    # 3删除
    xiaoming_dict.pop('name')
    # xiaoming_dict('name123')
    print(xiaoming_dict)


def dict_base2():
    """
    字典基本操作2
    :return:
    """
    xiaoming_dict = {"name": "小明", "age": 18}
    # 1. 统计键值对数量
    print(len(xiaoming_dict))
    # 2. 合并字典
    temp_dict = {"height": 1.75, "age": 20}
    # 注意：如果被合并的字典中包含已经存在的键值对，会覆盖原有的键值对
    xiaoming_dict.update(temp_dict)
    print(xiaoming_dict)
    # 3. 清空字典
    xiaoming_dict.clear()
    print(xiaoming_dict)


def iteration():
    """
    字典遍历
    :return:
    """
    xiaoming_dict = {"name": "小明", "qq": "123456", "phone": "10086"}
    for k in xiaoming_dict:
        print(k, xiaoming_dict[k])
    print("-" * 50)
    for kv in xiaoming_dict.items():  # 输出所有(key,value)元组列表
        print(kv)
    print("-" * 50)
    for k, v in xiaoming_dict.items():  # 拆包
        print(k, v)
    print("-" * 50)
    for k, v in xiaoming_dict.items():
        print(f'键 {k},值 {v}')
    print("-" * 50)
    for k in xiaoming_dict.keys():  # 只遍历键
        print(k)
    print("-" * 50)
    for v in xiaoming_dict.values():  # 只遍历值
        print(v)


def use_list_dict():
    """
    列表套字典
    :return:
    """
    card_list = [{"name": "张三", "qq": "12345", "phone": "110"}, {"name": "李四", "qq": "54321", "phone": "10086"}]
    for card in card_list:
        print(card)


def use_unpack_package():
    k, v, w = 1, 2, 3  # 一次定义多个变量的写法
    print(k, v, w)


if __name__ == '__main__':
    # dict_base()
    # dict_base2()
    iteration()
    # use_list_dict()
    # use_unpack_package()
